<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>修改部门</title>
    <link rel="stylesheet" href="../../../component/pear/css/pear.css"/>
</head>
<body>
<form class="layui-form layui-form-pane" action="" name="frmMain">
    <input type="hidden" name="id" >
    <div class="mainBox">
        <div class="main-container">
            <div class="layui-form-item ">
                <label class="layui-form-label">部门名称</label>
                <div class="layui-input-block">
                    <input type="text" name="dep_name" lay-verify="required" autocomplete="off" placeholder="请输入部门名称"
                           class="layui-input">
                </div>
            </div>
            <div class="layui-form-item ">
                <label class="layui-form-label">部门编号</label>
                <div class="layui-input-block">
                    <input type="text" name="dep_no" lay-verify="required" autocomplete="off" placeholder="请输入部门编号"
                           class="layui-input">
                </div>
            </div>
        </div>
    </div>
    <div class="bottom">
        <div class="button-container">
            <button type="submit" class="pear-btn pear-btn-primary pear-btn-sm" lay-submit=""
                    lay-filter="save">
                <i class="layui-icon layui-icon-ok"></i>
                提交
            </button>
        </div>
    </div>
</form>
<script src="../../../config/apiUrl.js"></script>
<script src="../../../component/layui/layui.js"></script>
<script src="../../../component/pear/pear.js"></script>
<script>
    async function initData(){
        let $ = layui.jquery;
        let common = layui.common;
        let toast = layui.toast;
        let id = common.getUrlParams("id");
        if (id != null) {
            let data = await common.ajax({
                url:apiUrlConfig.department.getById+id,
                type:"get"
            });
            common.setForm(data.data,$("[name='frmMain']"));
        }else{
            toast.error({title: false, message: "找不到数据请重新打开"})
        }
    }
    async function saveData(data){
        let $ = layui.jquery;
        let common = layui.common;
        let toast = layui.toast;
        try {
            let res_data = await common.ajax({
                url:apiUrlConfig.department.update,
                data:JSON.stringify(data)
            })
            parent.layui.toast.success({title: false, message: '保存成功'})
            const bc = new BroadcastChannel('depListReload');
            bc.postMessage(data);
            closeWin();
        }catch (result){
            if (result.status == 10001) {
                //字段提示
                let errorMsg = JSON.parse(result.msg);
                for (let key in errorMsg) {
                    layer.tips(errorMsg[key], $("[name='" + key + "']").parent(), {
                        tips: [3, '#f56c6c'], time: 5000, tipsMore: true
                    });
                }
            }
        }
    }
    function closeWin() {
        let layer_index = parent.layer.getFrameIndex(window.name);
        if (layer_index > 0) {
            parent.layer.close(layer_index); //关闭当前页
        }
    }
    layui.use(['form', 'jquery', 'http', 'popup', 'common', 'toast', 'dtree'], function () {
        let form = layui.form;
        let $ = layui.jquery;
        let http = layui.http;
        let popup = layui.popup;
        let common = layui.common;
        let toast = layui.toast;
        initData();

        $(document).keyup(function (event) {
            if (event.keyCode == 27) {
                closeWin();
            }
        });

        form.on('submit(save)', function (data) {
            saveData(data.field)
            return false;
        });
    })
</script>
<script>
</script>
</body>
</html>
